package vip.meeet.algorithm.str;


import org.junit.jupiter.api.Test;

public class Num7 {

    public static void main(String[] args) {
        System.out.println(reverse(123));
        System.out.println(reverse(-123));
        System.out.println(reverse(-1234567899));
        System.out.println(reverse(-123456789));
        System.out.println(reverse(-2147483412));
        System.out.println(reverse(1534236469));
        System.out.println(reverse(Integer.MIN_VALUE));
        System.out.println(reverse(Integer.MAX_VALUE));
    }

    public static int reverse(int x) {
        if(x == 0 || x == Integer.MAX_VALUE || x == Integer.MIN_VALUE){
            return 0;
        }
        boolean neg = false;
        if(x < 0){
            neg = true;
            x = -x;
        }
        StringBuilder sb = new StringBuilder();
        sb.append(String.valueOf(x));
        String string = sb.reverse().toString();
        int rs = 0;
        if(string.length() >=10){
            if(checkOverflow(string,neg)){
                return 0;
            }
        }
        rs = Integer.parseInt(string);
        return neg ? -rs : rs;
    }

    private static boolean checkOverflow(String num,boolean neg){
//        int[] max = {7,4,6,3,8,4,7,4,1,2};
//        int[] min = {8,4,6,3,8,4,7,4,1,2};//2147483 412
        int[] max = {2,1,4,7,4,8,3,6,4,7};
        int[] min = {2,1,4,7,4,8,3,6,4,8};
        int[] data;
        if(neg){
            data = min;
        }else {
            data = max;
        }
        for(int i=0;i<num.length();i++){
            int intnum = num.charAt(i) - 48;
            if(intnum > data[i]){
                return true;
            }else if(intnum < data[i]){
                return false;
            }
        }
        return false;
    }

    @Test
    public void print(){
        int[] max = {2,1,4,7,4,8,3,6,4,7};
        System.out.println(Integer.MAX_VALUE);
        System.out.println(Integer.MIN_VALUE);
        System.out.println('4' - 48);
        for(int i=max.length-1;i>=0;i--){
//            System.out.print(max[i] + ",");
            System.out.print(max[i]);
        }

    }

    @Test
    public void mysql(){
        //换底公式：log(a,b) = log(c,b) / log(c,a)
        int degree = 16 * 1024 / 4;
//        int degree = 40;
        int lines = 3000 * 10000;
        int keyLen = 4;
        int nodes = lines * keyLen / (16 * 1024);
        double halfDegree = Math.ceil(degree / 2);
        System.out.println(Math.log(nodes * halfDegree) / Math.log(halfDegree));
        System.out.println(Math.log(nodes * degree) / Math.log(degree));
    }

}
